﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using OpenFrameworkCore.Controls;
using System.Windows;

namespace OpenFrameworkWriter.Controls
{
	public abstract class EditorBase : Control
	{
		/// <summary>
		/// Editor가 transform 되었을때 발생하는 이벤트
		/// </summary>
		public event EventHandler<TransformedEventArgs> Transformed;

		/// <summary>
		/// 선택됐을때
		/// </summary>
		public event EventHandler<StateChangedEventArgs> StateChanged;

		/// <summary>
		/// Editor가 속해있는 ControlEditContainer
		/// </summary>
		public ControlEditContainerBase ParentControlEditContainer
		{
			get;

			private set;
		}

		/// <summary>
		/// Editor의 Angle
		/// </summary>
		public ControlBase TargetControl
		{
			get
			{
				return (ControlBase)GetValue(TargetControlProperty);
			}

			set
			{
				SetValue(TargetControlProperty, value);
			}
		}

		public static readonly DependencyProperty TargetControlProperty = DependencyProperty.Register(
		   "TargetControl",
		   typeof(ControlBase),
		   typeof(EditorBase),
		   new PropertyMetadata(OnTargetControlPropertyChanged)
		   );

		private static void OnTargetControlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			// Get new and old value
			ControlBase oldValue = (ControlBase)e.OldValue;
			ControlBase newValue = (ControlBase)e.NewValue;

			((EditorBase)d).OnTargetControlChanged(oldValue, newValue);
		}

		/// <summary>
		/// OnTargetControlChanged
		/// 
		/// EditorBase를 상속받은 클래스에서 구현할 수 있도록 한다.
		/// </summary>
		protected virtual void OnTargetControlChanged(ControlBase oldControl, ControlBase newControl)
		{
		}

		/// <summary>
		/// Editor의 상태를 나타냅니다.
		/// </summary>
		public EditorState State
		{
			get { return (EditorState)GetValue(StateProperty); }
			set { SetValue(StateProperty, value); }
		}

		/// <summary> 
		/// IsEnable Dependency Property
		/// </summary>
		public static readonly DependencyProperty StateProperty =
			DependencyProperty.Register(
				"State",
				typeof(EditorState),
				typeof(EditorBase),
				new PropertyMetadata(OnStatePropertyChanged));

		/// <summary> 
		/// IsEnableProperty property changed handler.
		/// </summary> 
		/// <param name="d"></param>
		/// <param name="e"></param>
		private static void OnStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			(d as EditorBase).OnStatePropertyChanged(e);

			// 이벤트 발생
			if ((d as EditorBase).StateChanged != null)
				(d as EditorBase).StateChanged(d, new StateChangedEventArgs() { ChangedState = (EditorState)e.NewValue });
		}

		/// <summary>
		/// OnStatePropertyChanged property changed handler.
		/// </summary>
		/// <param name="e"></param>
		protected virtual void OnStatePropertyChanged(DependencyPropertyChangedEventArgs e)
		{
		}

		/// <summary>
		/// 생성자
		/// </summary>
		/// <param name="parentEditContainer">Editor가 속해있는 부모 EditContainer</param>
		public EditorBase(ControlEditContainerBase parentEditContainer)
		{
			// ParentControlEditContainer 설정
			ParentControlEditContainer = parentEditContainer;
		}
	}
}
